﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand
{
    public class RetrieveNewCustomerServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerRequest, RetrieveCustomerResponse>
    {
        #region Properties
        public UserRecord Dao;
        public FilterRecord TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveNewCustomerServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            TextSearch = Request.Filter;
        }

        protected override bool OnExecute(RetrieveCustomerResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<CustomerRecord> records = new List<CustomerRecord>();
                List<Customers> listCustomer = new List<Customers>();
                Dictionary<string, Branch> listBranches = Providers.Branchs.RetrieveAllBranches().ToDictionary(x => x.BranchId);
                listCustomer = Providers.Customers.RetrieveCustomerByDao(out totalRecord, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                if (listCustomer != null)
                {
                    records = listCustomer.Select(x => new CustomerRecord
                    {
                        Zone = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].Region : string.Empty,
                        DaoName = x.DaoName,
                        Dao = x.Dao,
                        Branch = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId].BranchNameSme : string.Empty,
                        CIF = x.Cif,
                        Name = x.CusName,
                        VpbIndustry = x.IndustryName,
                        VpbIndustryEn = x.IndustryNameEn,
                        DateOpenCif = x.CusOpenDate.HasValue ? x.CusOpenDate.Value : DateTime.MinValue
                    }).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }


    }
}
